package process;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.swing.JOptionPane;
import main.SettingPanel;

public class Export
{
//Xuất cấu hình ra file xml
    public static void toXml(String filename)
    {
        try
        {
            File f = new File(filename);
            if (!f.exists())
            {
                f.createNewFile();
            }

            FileWriter fw = new FileWriter(filename);
            BufferedWriter bw = new BufferedWriter(fw);
            try (PrintWriter pw = new PrintWriter(bw))
            {
                pw.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
                pw.println("<connection>");
                pw.println("   <hostname>" + SettingPanel.hostname + "</hostname>");
                pw.println("   <port>" + SettingPanel.port + "</port>");
                pw.println("   <username>" + SettingPanel.username + "</username>");
                pw.println("   <password>" + SettingPanel.password + "</password>");
                pw.println("</connection>");
            }
        }
        catch (FileNotFoundException fex)
        {
            JOptionPane.showInternalMessageDialog(null, fex.toString());
        }
        catch (IOException ioex)
        {
            JOptionPane.showInternalMessageDialog(null, ioex.toString());
        }
        catch (Exception ex)
        {
            JOptionPane.showInternalMessageDialog(null, "Error to export to " + filename + " with XML format");
        }
    }
}
